// Copyright 2011 Google Inc. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.


/**
 * @fileoverview Provides functionality for the entire code base.
 *
 * Public functions/enums:
 *   shellExtension.utils.inEnum(data, obj) - Checks if the inputs are of the
 *       correct types and that data is in obj.
 *   shellExtension.utils.isType(data, type) - Checks if data is of the given
 *       type.  It checks that type is a string and data is only allowed to be
 *       null if type is 'null'.
 *   shellExtension.utils.isTypeArray(data, type) - Checks that the inputs are
 *       the correct type, the data is an object with a 'length' property and
 *       that each sequential element is of the correct type.
 *
 * @author jasonstredwick@google.com (Jason Stredwick)
 */


goog.provide('shellExtension.utils');


/**
 * Verifies if the data is a value in an enum.  Uses isType to verify the type
 * of data and obj.
 * @param {string} data The data to check.
 * @param {!Object.<string>} obj The object to test.
 * @return {boolean} Whether data is a value in obj.
 */
shellExtension.utils.inEnum = function(data, obj) {
  if (!shellExtension.utils.isType(data, 'string') ||
      !shellExtension.utils.isType(data, 'object')) {
    return false;
  }

  for (var key in obj) {
    if (data === obj[key]) { // ensure object's value is a string.
      return true;
    }
  }
  return false;
};


/**
 * Verifies the given data as the specified type.  Ensures that type is a
 * string.  It also has special handling features for 'null'.  If null is
 * specified then the type must be 'null' or the function will return false.
 * @param {*} data The data to check.
 * @param {string} type The type to check against.
 * @return {boolean} Whether the data is of the given type.
 */
shellExtension.utils.isType = function(data, type) {
  if (typeof(type) != 'string') {
    return false;
  } else if (data === null && type != 'null') {
    return false;
  }
  return typeof(data) == type;
};


/**
 * Checks if data is an array of strings.  Uses isType to test the type of
 * data and it's elements' type.
 * @param {*} data The data to validate.
 * @param {string} type The type of array to check for.
 * @return {boolean} Whether the data is valid.
 */
shellExtension.utils.isTypeArray = function(data, type) {
  if (shellExtension.utils.isType(data, 'object') && 'length' in data) {
    for (var i = 0, len = data.length; i < len; ++i) {
      if (!shellExtension.utils.isType(data[i], type)) {
        return false;
      }
    }
    return true;
  } else {
    return false;
  }
};


/**
 * Converts a string to an Array of strings or nothing if the input is already
 * an array of strings.  Validation of inputs occurs as the inputs can come
 * from external sources.
 * @param {!Array.<string>|string} input The input to convert.
 * @return {Array.<string>} The array of strings or null upon error.
 */
shellExtension.utils.toStringArray = function(input) {
  if (typeof(input) == 'string') {
    return [input];
  } else if (shellExtension.utils.isTypeArray(input, 'string')) {
    return input;
  }

  return null;
};

